<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="合同编号" prop="contractNumber">
        <el-input v-model="queryParams.contractNumber" clearable />
      </el-form-item>
      <el-form-item label="房间" prop="premisesAddress">
        <el-input v-model="queryParams.premisesAddress" clearable />
      </el-form-item>
      <el-form-item label="状态" prop="contractStatus">
        <el-select
          v-model="queryParams.contractStatus"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="dict in dict.type.proprietor_contract_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同类型" prop="cooperationType">
        <el-select
          v-model="queryParams.cooperationType"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="dict in dict.type.cooperation_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="签署方式" prop="signingMode">
        <el-select
          v-model="queryParams.signingMode"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="dict in dict.type.signing_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['enterprise:proprietorContract:add']"
        >新增
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
      ref="table"
    >
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="合同编号" prop="contractNumber">
      </el-table-column>
      <el-table-column label="房间" prop="premisesAddress">
      </el-table-column>
      <el-table-column label="合同类型" prop="cooperationType">
        <template #default="scope">
          <dict-tag
            :options="dict.type.cooperation_type"
            :value="scope.row.cooperationType"
          />
        </template>
      </el-table-column>
      <el-table-column label="签署方式" prop="signingMode">
        <template #default="scope">
          <dict-tag
            :options="dict.type.signing_mode"
            :value="scope.row.signingMode"
          />
        </template>
      </el-table-column>
      <el-table-column label="合同模板" prop="contractTemplateName"/>
      <el-table-column
        label="最晚签约日期"
        align="center"
        prop="latestSigningDate"
      >
      </el-table-column>
      <el-table-column label="状态" prop="contractStatus">
        <template #default="scope">
          <dict-tag
            :options="dict.type.proprietor_contract_status"
            :value="scope.row.contractStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDeliveryOrder(scope.row)"
            :disabled="scope.row.addDeliveryOrderStatus !== '1'"
            v-hasPermi="['enterprise:deliveryOrder:add']"
          >新增设施清单
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['enterprise:proprietorContract:query']"
          >查看合同
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleSignProgress(scope.row)"
            v-hasPermi="['enterprise:proprietorContract:query']"
            v-if="scope.row.signingMode == 1"
          >签署进度
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleSign(scope.row)"
            v-hasPermi="['enterprise:proprietorContract:signing']"
            v-if="scope.row.signingMode == 1 && scope.row.contractStatus == 1 && scope.row.partyASignFlag == 0"
          >合同签署
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleViewQrcode(scope.row)"
            v-hasPermi="['enterprise:proprietorContract:signing']"
            v-if="scope.row.signingMode == 1 && scope.row.contractStatus == 1"
          >签署二维码
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDownload(scope.row)"
            v-hasPermi="['enterprise:proprietorContract:query']"
            v-if="scope.row.signingMode == 2 && scope.row.contractStatus == 1"
          >下载合同
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpload(scope.row)"
            v-hasPermi="['enterprise:proprietorContract:upload']"
            v-if="scope.row.signingMode == 2 && scope.row.contractStatus == 1"
          >上传合同
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleRepeal(scope.row)"
            v-hasPermi="['enterprise:proprietorContract:repeal']"
            v-if="scope.row.contractStatus == 1 "
          >撤销
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增 -->
    <Add v-model="addDialogVisible" :title="title" @success="handleQuery"/>
    <!-- 详情 -->
    <Detail
      v-model="detailDialogVisible"
      :title="title"
      :contractId="id"
      :isDetail="isDetail"
    />
    <!-- 签署进度 -->
    <SignProgress
      v-model="signProgressDialogVisible" :title="title" :data="form"
    />
    <!-- 查看签署二维码 -->
    <ViewQrcode :data="form" :title="title" v-model="viewQrcodeDialogVisible"/>
    <!-- 查看合同 -->
    <ViewContract :data="form" :title="title" v-model="viewDialogVisible"/>
    <!-- 上传合同 -->
    <UploadContract v-model="uploadContractDialogVisible" :title="title" :data="form" @success="handleQuery"/>
    <!--  添加设施清单弹窗  -->
    <AddDeliveryOrder v-model="deliveryOrderDialogVisible" :title="title" :formData="form" contract-type="1" @success="handleQuery"/>
  </div>
</template>

<script>

import Add from './components/Add.vue'
import Detail from './components/Add.vue'
import ViewContract from './components/view-contract.vue'
import SignProgress from './components/sign-progress.vue'
import UploadContract from './components/upload-contract.vue'
import ViewQrcode from './components/view-qrcode.vue'
import {
  listProprietorContract,
  previewElectronicVisaUrl,
  repealProprietorContract,
  signingProprietorContract
} from '@/api/contract/proprietor'
import AddDeliveryOrder from '@/views/long-short-rent/contract/deliveryOrder/components/add.vue'


export default {
  name: 'contract',
  dicts: ['proprietor_contract_status', 'cooperation_type', 'signing_mode'],
  components: {
    AddDeliveryOrder,
    SignProgress,
    Add,
    Detail,
    ViewContract,
    UploadContract,
    ViewQrcode
  },
  data() {
    return {
      //统计数据
      statistics: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      list: [],
      // 弹出层标题
      title: '',
      //表格行id
      id: undefined,
      //是否详情页
      isDetail: false,
      // 是否显示弹出层
      open: false,
      // 当前选择
      current: '0',
      // 日期范围
      dateRange: [],
      // 是否显示新建合同弹窗
      addDialogVisible: false,
      // 是否显示合同详情弹窗
      detailDialogVisible: false,
      // 是否显示查看合同弹窗
      viewDialogVisible: false,
      // 是否显示签署进度弹窗
      signProgressDialogVisible: false,
      // 是否显示上传合同弹窗
      uploadContractDialogVisible: false,
      // 是否显示查看签署二维码弹窗
      viewQrcodeDialogVisible: false,
      // 新增设施清单弹窗
      deliveryOrderDialogVisible: false,
      src: 'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNumber: undefined, //合同编号
        premisesAddress: undefined, //房间
        contractStatus: undefined, //合同状态:1.签署中;2.签署完成;3.已终止;4.已逾期(字典字段:proprietor_contract_status)
        cooperationType: undefined,//合作方式:1.房屋委托;2.代理服务(字典字段:cooperation_type)
        signingMode: undefined//签署方式:1.电子合同;2.纸质合同(字典字段:signing_mode)
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  methods: {
    //关闭回调
    dialogClose() {
      this.openCheckOut = false
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      listProprietorContract(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.list = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    // 表单重置
    reset() {
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.contractId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.addDialogVisible = true
      this.title = '新增业主合同'
    },
    handleDetail(row) {
      this.detailDialogVisible = true
      this.title = '详情'
      this.id = row.contractId
      this.isDetail = true
    },
    /** 查看按钮操作 */
    handleView(row) {
      if (row.signingMode == 1) {
        let params = {
          proprietorContractId: row.proprietorContractId,
          redirectUrl: ''
        }
        previewElectronicVisaUrl(params).then(
          (response) => {
            window.open(response.data)
          }
        )
        return
      }
      this.form = { ...row }
      this.viewDialogVisible = true
      this.title = '查看合同'
    },
    // 签署进度
    handleSignProgress(row) {
      this.title = '签署进度'
      this.form = { ...row }
      this.signProgressDialogVisible = true
    },
    // 合同签署
    handleSign(row){
      let params = {
        proprietorContractId: row.proprietorContractId,
        redirectUrl: ''
      }
      signingProprietorContract(params).then((response) => {
        window.open(response.data)
      })
    },
    // 查看签署二维码
    handleViewQrcode(row) {
      this.form = { ...row }
      this.title = '签署二维码'
      this.viewQrcodeDialogVisible = true

    },
    // 下载合同
    handleDownload(row) {
      let params = {
        proprietorContractId: row.proprietorContractId
      }
      this.download(
        "/enterprise/contract/proprietorContract/export",
        {
          ...params,
        },
        `${row.contractTemplateName}.doc`
      );
    },
    // 上传合同
    handleUpload(row) {
      this.form = { ...row }
      this.title = '上传合同'
      this.uploadContractDialogVisible = true
    },
    // 撤销
    handleRepeal(row) {
      this.$confirm('确定撤销该合同吗？').then(() => {
        // 调用撤销接口
        return repealProprietorContract({ proprietorContractId: row.proprietorContractId }).then((res) => {
          this.$message.success(res.msg)
          this.getList()
        }).catch(() => {
          this.$message.error('撤销失败')
        })
      }).catch(() => {
        console.log('取消撤销')
      })
    },
    // 新增设施清单
    handleDeliveryOrder(row) {
      this.form = { ...row }
      this.form['deliveryOrderType'] = '1'
      this.form['relatedContractId'] = row.proprietorContractId
      this.title = '新增设施清单'
      this.deliveryOrderDialogVisible = true
    }
  }
}
</script>
<style lang="scss" scoped>
.title {
  font-size: 16px;
  font-weight: bold;
  color: #666;
}

.total {
  font-size: 24px;
  font-weight: 500;
  color: #333;
  margin-top: 6px;
}

.left-border {
  border-left: 1px solid #999;
}

.right-border {
  border-right: 1px solid #999;
}

.info {
  padding: 10px 15px;
  margin: 15px 0;
  background: #e6f7ff;
  color: #1890ff;

  span {
    color: #565c5f;
  }

  .clear {
    margin-left: 20px;
    cursor: pointer;
  }
}

.QR-code {
  padding: 30px;
  width: 100%;
  display: flex;
  justify-content: center
}
</style>
